package hos.base;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.text.TextUtils;

import hos.utils.LogH;


/**
 * <p>Title: LoadingDialog </p>
 * <p>Description: 对话框 </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023-02-02 16:05
 */
public class LoadingDialog implements IViewLoading {
    private Dialog mProgressDialog;

    public LoadingDialog(Dialog dialog) {
        this.mProgressDialog = dialog;
    }

    @Override
    public void showLoading(int titleId) {
        if (mProgressDialog == null) {
            return;
        }
        showLoading(mProgressDialog.getContext().getResources().getString(titleId));
    }

    @Override
    public void showLoading(String title, boolean isDismissOnBackPressed, boolean isDismissOnTouchOutside) {
        if (mProgressDialog == null) {
            return;
        }
        Class<? extends Dialog> target = mProgressDialog.getClass();
        if (TextUtils.equals(target.getName(), "androidx.appcompat.app.AlertDialog")) {
            try {
                target.getMethod("setMessage", String.class).invoke(mProgressDialog, title);
            } catch (Exception e) {
                LogH.e("activity", e);
            }
        } else if (mProgressDialog instanceof AlertDialog) {
            ((AlertDialog) mProgressDialog).setMessage(title);
        } else {
            mProgressDialog.setTitle(title);
        }


        mProgressDialog.setCanceledOnTouchOutside(isDismissOnTouchOutside);
        mProgressDialog.setCancelable(isDismissOnBackPressed);
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }

    @Override
    public void loadingStyle(boolean isLoading) {
        if (mProgressDialog == null) {
            return;
        }
        if (isLoading) {
            if (mProgressDialog instanceof ProgressDialog) {
                ((ProgressDialog) mProgressDialog).setProgressStyle(ProgressDialog.STYLE_SPINNER);
            }
            return;
        }
        if (mProgressDialog instanceof ProgressDialog) {
            ((ProgressDialog) mProgressDialog).setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            int max = ((ProgressDialog) mProgressDialog).getMax();
            if (max != 100) {
                ((ProgressDialog) mProgressDialog).setMax(100);
            }
            ((ProgressDialog) mProgressDialog).setProgress(0);
        }
    }

    @Override
    public void loadingProgress(int progress) {
        if (mProgressDialog == null) {
            return;
        }
        if (mProgressDialog instanceof ProgressDialog) {
            ((ProgressDialog) mProgressDialog).setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            int max = ((ProgressDialog) mProgressDialog).getMax();
            if (max != 100) {
                ((ProgressDialog) mProgressDialog).setMax(100);
            }
            ((ProgressDialog) mProgressDialog).setProgress(progress);
        }
    }

    @Override
    public void hideLoading() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void onDestroy() {
        hideLoading();
        mProgressDialog = null;
    }
}
